import md5 from 'md5'
import pagination from '@/components/pagination.vue'
import { Request, confirm, successToast, errorToast, checkPermission } from '@/utils'
import { storeTypeList } from '@/config'

let request = new Request()

export default {
	name : 'store',
	data () {
		return {
			filter : {
				store : '',
				searchType : 1,
				storeType : ''
			}, 
			
			pageQuery : {
				
			},
			
			showResetPwdDiaLog : false,
			newPwd : '',
			currentAccount : '',
			
			storeTypeList : storeTypeList,
			storeList : [],
			
			currentstore : {},
			showForbiddenDiaLog : false, //禁用代理商窗口
		}
	},
	
	components : {
		pagination
	},
	
	methods : {
		checkPermission : checkPermission,
		search : function(){
			let filter = Object.assign({}, this.filter)
			if(filter.searchType == 1){
				filter.storeName = filter.store
			}else{
				filter.storeAccount = filter.store
			}
			
			delete filter.searchType
			delete filter.store
			
			this.pageQuery = Object.assign({}, filter)
			
			this.$nextTick(() => {
				this.$refs.pagination.initPage()
			})

		},
		
		page : function(rows){
			this.deviceList.splice(0)
			rows.forEach(v => {
				this.deviceList.push(v)
			})
		},
		
		deletestore : function(storeId){ //删除代理商
			confirm.call(this, '确定删除该代理商吗', () => {
				//request.get('store/delete')
			})
		},
		
		toggleStatus : function(scope){
			let status = scope.status == 1 ? 0 : 1
			request.post('store/setstatus', res => {
				if(res.success){
					scope.status = status
				}else{
					errorToast.call(this, res.msg)
				}
			}, {id : scope.store_id, status : status})
		},
		
		openResetPwd : function(account){
			this.newPwd = ''
			this.showResetPwdDiaLog = true
			this.currentAccount = account
		},
		
		resetPwd : function(){
			request.put('store/storeResetPwd', res => {
				if(res.code == 0){
					this.showResetPwdDiaLog = false
					successToast('重置成功', this)
				}else{
					errorToast(res.msg, this)
				}
			}, {storeAccount : this.currentAccount, password : md5(this.newPwd)})
		},
		
		openForbidden : function(store){
			this.showForbiddenDiaLog = true
			this.currentstore = store
		},
		
		changeStatus : function(){
			let store = this.currentstore
			let status = store.status == 0 ? 1 : 0
			request.put(`store/changeStatus?storeAccount=${store.storeAccount}&status=${status}`, res => {
				if(res.code == 0){
					this.showForbiddenDiaLog = false
					store.status = status
					successToast('修改成功', this)
				}else{
					errorToast(res.msg, this)
				}
			}, {storeAccount : store.storeAccount, status : status})
		}
			
	},
	
	activated () {
		if(!this.ISFIRST){
			this.ISFIRST = true
		}else{
			this.$refs.pagination.getPage()
		}
	}
}